/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.panels;


import delta.Calculations;
import model.Epoxy;
import model.Fiber;
import persistence.Storage;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;


/**
 *
 * @author Memet
 */
public class FiberEpoxyPanel extends javax.swing.JPanel {

    private FiberOperationPanel fiberOperations;
    private EpoxyOperationPanel epoxyOperations;
    private JPanel operations;
    private List<Epoxy> epoxies;
    private List<Fiber> fibers;



    public FiberEpoxyPanel() {
        initComponents();
        fiberOperations = new FiberOperationPanel();
        epoxyOperations = new EpoxyOperationPanel();
        operations = fiberOperations; //wtf
        epoxies = Storage.getAllEpoxy();
        fibers = Storage.getAllFibers();

        updateForm();
    }

    public void updateContents(){
        epoxies = Storage.getAllEpoxy();
        fibers = Storage.getAllFibers();
        updateForm();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblFiber = new javax.swing.JTable();
        jScrollPane3 = new javax.swing.JScrollPane();
        tblEpoxy = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();

        setMaximumSize(new java.awt.Dimension(509, 336));
        setMinimumSize(new java.awt.Dimension(509, 336));

        jLabel1.setFont(new java.awt.Font("Trebuchet MS", 1, 24)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Fiber");

        jScrollPane1.setBorder(null);

        tblFiber.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null}
            },
            new String [] {
                "Name", "Manufacturer", "E [GPa]", "v", "<html>ρ [Kg/m<sup>3</sup>]</html>", "Color"
            }
        ));
        tblFiber.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblFiberMouseClicked(evt);
            }
        });

        jScrollPane1.setViewportView(tblFiber);

        jScrollPane3.setBorder(null);

        tblEpoxy.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Name", "Manufacturer", "E [GPa]", "v", "<html>ρ [Kg/m<sup>3</sup>]</html>"
            }
        ));
        tblEpoxy.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblEpoxyMouseClicked(evt);
            }
        });
        jScrollPane3.setViewportView(tblEpoxy);

        jLabel2.setFont(new java.awt.Font("Trebuchet MS", 1, 24)); // NOI18N
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("Epoxy");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 509, Short.MAX_VALUE)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 509, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 489, Short.MAX_VALUE)
                .addContainerGap())
            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 509, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 128, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void tblFiberMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblFiberMouseClicked
        if (tblEpoxy.getSelectedRow() != -1) {
            tblEpoxy.clearSelection();
        }
        updateForm();
    }//GEN-LAST:event_tblFiberMouseClicked

    private void tblEpoxyMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblEpoxyMouseClicked
        if (tblFiber.getSelectedRow() != -1) {
            tblFiber.clearSelection();
        }
        updateForm();
    }//GEN-LAST:event_tblEpoxyMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTable tblEpoxy;
    private javax.swing.JTable tblFiber;
    // End of variables declaration//GEN-END:variables

    public JPanel getOperations(){
        return operations;//wtf?
    }

    private void updateForm(){
        int selectionf = tblFiber.getSelectedRow();
        int selectione = tblEpoxy.getSelectedRow();
        //fiber tabel updaten
        DefaultTableModel model = new DefaultTableModel(
                new Object[][]{},
                new String[]{
                    "Name", "E [GPa]", "v", "ρ [Kg/m3]", "Manufacturer", "Plot color"
                });
        tblFiber.setModel(model);

        // populate fiber table
        for(Fiber fiber : fibers){
            String rgb = Integer.toHexString(fiber.getColor().getRGB());
            rgb = rgb.substring(2, rgb.length());
            model.addRow(new Object[]{fiber, Calculations.Round(fiber.getE() * 1e-9, 2),fiber.getMu(),fiber.getRho(),fiber.getManufacturer(), "<html><p bgcolor=\"#"+rgb+"\"></p></html>"});

        }

        if(selectionf==-1){ // no row selected
            //pnlEditF.setVisible(false);
        } else {
            Fiber fib = (Fiber) tblFiber.getValueAt(selectionf, 0);
            fiberOperations.setFiber(fib);
            operations = fiberOperations;
        }
        tblFiber.changeSelection(selectionf, 0, false, false);

        //epoxy tabel updaten
        DefaultTableModel emodel = new DefaultTableModel(
                new Object[][]{},
                new String[]{
                    "Name", "E [GPa]", "v", "ρ [Kg/m3]", "Manufacturer", "Plot color"
                });
        tblEpoxy.setModel(emodel);

        //populate epoxy table
        for(Epoxy epoxy : epoxies){
            String rgb = Integer.toHexString(epoxy.getColor().getRGB());
            rgb = rgb.substring(2, rgb.length());
            emodel.addRow(new Object[]{epoxy,Calculations.Round(epoxy.getE()*1e-9,2),epoxy.getMu(),epoxy.getRho(),epoxy.getManufacturer(), "<html><p bgcolor=\"#"+rgb+"\"></p></html>"});
        }


        if(selectione==-1){

            //pnlEditE.setVisible(false);
        } else {
            Epoxy epo = (Epoxy) tblEpoxy.getValueAt(selectione, 0);
            epoxyOperations.setEpoxy(epo);
            operations = epoxyOperations;
        }
        tblEpoxy.changeSelection(selectione, 0, false, false);

        operations.repaint();
        operations.revalidate();
        /*
        todo refactor

        Global.menu.repaint();
        Global.menu.validate();
        Global.menu.setOperations(operations);
         */
    }

    public void clearEpoxySelection() {
        tblEpoxy.clearSelection();
    }

    void clearFiberSelection() {
        tblFiber.clearSelection();
    }
}
